package cn.lzb.book.server.dao.repository;

import cn.lzb.book.server.dao.mapper.BookMapper;
import cn.lzb.book.server.dao.po.BookPo;
import cn.lzb.book.server.entity.dto.BookPageQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liuzhibo
 * @date 2024/8/28 16:28
 **/
@Repository
public class BookRepository extends ServiceImpl<BookMapper, BookPo> implements IService<BookPo> {

    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Long> ids) {
        UpdateWrapper<BookPo> updateWrapper = new UpdateWrapper<BookPo>()
                .in("id", ids)
                .set("deleted", true);
        this.update(updateWrapper);
    }


    public Page<BookPo> queryPage(BookPageQuery bookPageQuery) {
        QueryWrapper<BookPo> queryWrapper = new QueryWrapper<BookPo>()
                .like(StringUtils.isNotBlank(bookPageQuery.getName()), "name", bookPageQuery.getName())
                .like(StringUtils.isNotBlank(bookPageQuery.getCategoryCode()), "category_code", bookPageQuery.getCategoryCode())
                .between(StringUtils.isNotBlank(bookPageQuery.getPublishDateFrom())
                        && StringUtils.isNotBlank(bookPageQuery.getPublishDateTo()), "publish_date", bookPageQuery.getPublishDateFrom(), bookPageQuery.getPublishDateTo())
                .eq("deleted", false);
        return this.page(new Page<>(bookPageQuery.getPage(), bookPageQuery.getSize()), queryWrapper);
    }
}
